<?php
require_once './mysql.php';
class MySQL_location_path extends MySQL
{
    # 连接Mysql数据库
    function connect()
    {
        $conn = parent::connect();
        return $conn;
    }
    # 转换坐标
    public function trans_gis_a($longitude, $latitude)
    {
        $url = "https://restapi.amap.com/v3/assistant/coordinate/convert?locations={$longitude},{$latitude}&coordsys=gps&key=e2043f0ab803f1d78e8372ee124630c9";

        $res = file_get_contents($url);

        $json_dict = json_decode($res, true);

        //echo  $json_dict['locations'];

        return $json_dict['locations'];
    }

    # 查询用户ID
    public function get_userId($key)
    {
        return parent::get_userId($key);
    }

    //返回最新一条记录
    public function get_gis($ID)
    {
        $conn = $this->connect();
        // 执行查询的代码
        $table = $ID . '_location';
        $sql = "SELECT * FROM `$table` ORDER BY date DESC LIMIT 1;";
        // echo $sql;
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            // 输出数据
            while ($row = $result->fetch_assoc()) {
                $longitude = $row['Longitude'];
                $latitude = $row['Latitude'];
                $Long_lat_tri = $this->trans_gis_a($longitude, $latitude);
                //echo $Long_lat_tri;
                //return $longitude . "," . $latitude;
                return $Long_lat_tri;
            }
        } else {
            echo "SQL False";
        }
        $conn->close();
    }
}

function risk_area($ID)
{
    $db=new MySQL_location_path();
    $conn=$db->connect();
    $tit = array();
    $table = $ID . '_risk_district';
    $sql = <<<EOF
                    SELECT * FROM `$table` where is_true = 1;
          EOF;
    $ret = $conn->query($sql);
    while ($row = $ret->fetch_assoc()) {
        //echo count($row);//7
        $longitude_latitude = $row['Longitude_latiude'];
        $title = $row['risk_name'];
        $path = $title . "_path";
        echo 'var ' . $path . ' = [';
        foreach (explode("|", $longitude_latitude) as $val) {
            echo "[" . $val . "],";
            //$lo_la[]="[".$val."]";
        }
        echo '];';

        echo <<<POLYGON
                    var $title = new AMap.Polygon({
                    path: $path,
                    strokeColor: "#f13c22",
                    strokeWeight: 6,
                    strokeOpacity: 0.2,
                    fillOpacity: 0.4,
                    fillColor: '#f97d1c',
                    zIndex: 50,
                    bubble: true,
                    })
            POLYGON;
        echo ';';
        $tit[] = $title;
        //echo $title;
    }
    $polygon = '';
    foreach ($tit as $value) {
        $polygon = $polygon . $value . ",";
    }
    echo 'map.add([' . $polygon . ']);';
    $conn->close();
}

require_once './check.php';  //导入key检测函数
class check extends Check_key
{
    public function check_key()
    {
        return parent::check_key();
    }
}
$check = new check();
$ID = $check->check_key(); //如果key存在且正确，返回用户ID，否则会返回false
if ($ID != false) {
    
    risk_area($ID);

    $myObject = new MySQL_location_path();
    $Lat_Long = $myObject->get_gis($ID);
    $js = <<<EOF
map.setCenter([$Lat_Long]);

var loca = window.loca = new Loca.Container({
map,
});

// 红色呼吸点
var geoLevelF = new Loca.GeoJSONSource({
data: {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [$Lat_Long]
}
}]
},
});
var breathRed = new Loca.ScatterLayer({
loca,
zIndex: 113,
opacity: 1,
visible: true,
zooms: [2, 22],
});

breathRed.setSource(geoLevelF);
breathRed.setStyle({
unit: 'meter',
size: [30, 30],
borderWidth: 0,
texture: '../images/breath_red.png',
duration: 1000,
animate: true,
});

// 黄色呼吸点
/*
var geoLevelE = new Loca.GeoJSONSource({
data: {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [108.57106421046336, 31.02851485528644]
}
}]
},
//url: '../data/sz_road_E.json',
});
var breathYellow = new Loca.ScatterLayer({
loca,
zIndex: 112,
opacity: 1,
visible: true,
zooms: [2, 22],
});

breathYellow.setSource(geoLevelE);
breathYellow.setStyle({
unit: 'meter',
size: [20, 20],
borderWidth: 0,
texture: '../images/breath_yellow.png',
duration: 1000,
animate: true,
});
*/
// 启动渲染动画
loca.animate.start();
EOF;
    echo $js;
} else { //token错误
    $url = "../index.php";
    echo "<script type='text/javascript'>";
    echo "window.location.href='$url'";
    echo "</script>";
    exit;
}
